package com.test.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.user.service.TyplmUserService;
import com.hustcad.plm.pdm.workflow.model.vo.AdvancedFlowContainerVO;
import com.test.service.CustomWorkItemService;
import com.ty.basic.constant.ApiPathConstant;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "流程任务服务")
@Slf4j
@RequestMapping(ApiPathConstant.PRODUCT_VERSION_ONE_PATH + "workitem")
public class CustomWorkItemController {
    @Resource
    private CustomWorkItemService customWorkItemService;
    @Resource
    private TyplmUserService typlmUserService;


    /**
     * 根据Oid获取文件夹信息
     *
     * @return
     */
    @PostMapping("/batchTransfer")
    @ApiOperation(value = "batchTransfer", produces = "application/json; charset=utf-8")
    public ResponseResult batchatchTransfer(@RequestBody Map<String, Object> dataMap) {
        ResponseResult result = new ResponseResult<>();
        try {
            List<AdvancedFlowContainerVO> advancedFlowContainerVOList = new ArrayList<>();
            List<Object> list = (List<Object>) dataMap.get("advancedFlowContainerVOList");
            for (Object obj : list) {
                // 将list中的数据转成json字符串
                String jsonObject= JSON.toJSONString(obj);
                //将json转成需要的对象
                advancedFlowContainerVOList.add(JSONObject.parseObject(jsonObject, AdvancedFlowContainerVO.class));
            }
            BigInteger userId = new BigInteger(dataMap.get("userId").toString()); // 字符串转BigInteger
            String remark = dataMap.get("remark").toString();

            log.info("================userId:{}", userId);
            log.info("================remark:{}", remark);
            log.info("================advancedFlowContainerVOList.size:{}", advancedFlowContainerVOList.size());

            customWorkItemService.batchTransfer(result, advancedFlowContainerVOList, userId, remark);
        } catch (Exception e) {
            result.setSuccess(Boolean.FALSE);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping("/queryUserByName")
    @ApiOperation(value = "queryUserByName", produces = "application/json; charset=utf-8")
    public ResponseResult<List<UserDO>> queryUserByName(@RequestBody String userName) {
        ResponseResult<List<UserDO>> result = new ResponseResult<>();
        try {
            log.info("================queryUserByName:{}", userName);
            customWorkItemService.queryUserByName(result, userName);
        } catch (Exception e) {
            result.setSuccess(Boolean.FALSE);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
